package com.bjtang.fileSync;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;

import com.bjtang.fileSync.file.FileMonitor;
import com.bjtang.fileSync.nio.NIOServer;
import com.bjtang.fileSync.util.FileConfig;

public class FileSyncApp {

	public static void main(String[] args) throws Exception {
		startlog4j();

		// 每两秒监听一次,实际上最好每分钟进行监听
		String readConfig = FileConfig.readConfig("role").trim();
		if ("client".equals(readConfig)) {
			FileMonitor fileMonitor = new FileMonitor();
			fileMonitor.start();
		} else {
			// 启动监听服务器
			NIOServer.main(null);
		}

	}

	public static void startlog4j() {
		try {
			File file = new File(FileConfig.getLogConfigPath());
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
			ConfigurationSource source;
			source = new ConfigurationSource(in);
			Configurator.initialize(null, source);
		} catch (IOException e) {
			System.err.println("日志系统启动失败!");
		}
	}
}
